<template>
  <div class="app-container">
    <SpiderSubscribeSearchBar v-model:name="query.name" />
    <el-card class="box-card mt">
      <el-button type="primary"  @click="onAdd">
        <el-icon :size="20" class="mr-1">
          <IconIcOutlineAdd />
        </el-icon>添加
      </el-button>

      <SpiderSubscribeAdd
       :data="current"
       v-model:isShow="isShow" />


      <el-table :data="list" style="width: 100%" v-loading="loading" class="mt">
        <el-table-column prop="hostName" label="站点" />
        <el-table-column prop="name" label="书名" />
        <el-table-column prop="author" label="作者" />
        <el-table-column prop="instalment" label="连载" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column prop="lastChapter" label="最新章节" />
        <el-table-column prop="status" label="入库状态" >
          <template #default="scope">
              {{ getStatus(scope.row) }}
            </template>
        </el-table-column>

         <el-table-column align="center" label width="250">
            <template #default="scope">
              <ComponentCrud :data="scope" @onEdit="onEdit" @onRestore="onRestore" @onDelete="onDelete" >
                <el-button type="primary" @click="onReset(scope.row)">重置</el-button>
              </ComponentCrud>
            </template>
          </el-table-column>
      </el-table>


      <ComponentPagination :total="total" v-model:page-size="query.pageSize" v-model:page="query.currentPage" />
    </el-card>

  </div>
</template>

<script>
import { queryData, reset, deletes, restore } from '@/api/novel/index'
import { useDictionaryStore } from "@/store/dictionary"

export default {
  name: "dictionary",
  data() {
    return {
      query: {
        name: '',
        currentPage: 1,
        pageSize: 10
      },
      list: null,
      total: 0,
      loading: true,
      isShow: false,
      current: null
    };
  },
  computed: {
    ...mapState(useDictionaryStore, ['bookDict']),
    getStatus() {
      return function(row) {
        const item = this.bookDict.subscribe.find(t=>t.itemCode == row.status)
        return item.itemName
      }
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    async getInfo() {
      this.loading = true
      try {
        var res = await queryData(this.query)

        this.list = res.result.data;
        this.total = res.result.total;
        this.loading = false;
      }
      finally {
        this.loading = false
      }
    },
    onAdd() {
      this.isShow = true;
    },
    onEdit(row) {
      this.isShow = true;
      this.current = row;
    },
    onDelete(row) {
      deletes(row.id).then((_) => {
            row.isDeleted = true;
          });
    },
    onRestore(row) {
      restore(row.id).then((_) => {
        
            row.isDeleted = false;
          });
    },
    onReset(row) {
      reset(row.id)
    },
  },
  mounted: function () {},
  watch: {
    query: {
      handler(val, oldVal) {
      this.getInfo();
      },
      deep: true
    },
  }
}
</script>
